## Author - Matthew Stanton
## 4-26-08
## Main program for application

         .text
         .globl main
 
main:
         #prolog
         sub     $sp,$sp,4        # push the return address
         sw      $ra,($sp)
         sub     $sp,$sp,4        # push $a1
         sw      $a1,($sp)
         sub     $sp,$sp,4        # push $a2
         sw      $a2,($sp)
         
         #body
         jal getSize              # calls method
         jal writeBox             # calls method

         #epilog
         lw      $a2,($sp)        # pop $a2
         add     $sp,$sp,4
         lw      $a1,($sp)        # pop $a1
         add     $sp,$sp,4
         lw      $ra,($sp)        # pop return address
         add     $sp,$sp,4
         jr      $ra              # return to OS

## getSize -- determines dimension of box
##
## User Input:
## $a1 -- number of rows
## $a2 -- number of colums

         .text
         .globl getSize

getSize:  

         # body
         la      $a0,nrows        # prompt the user for number of rows
         li      $v0,4            # service 4
         syscall
         li      $v0,5            # service 5 -- read int
         syscall                  # $v0 = integer
         move    $a1,$v0          # save rows
         
         la      $a0,ncols        # prompt the user
         li      $v0,4            # service 4
         syscall
         li      $v0,5            # service 5 -- read int
         syscall
         move    $a2,$v0          # save columns

         # epilog
         jr      $ra              # return to caller

         .data
nrows:   .asciiz  "How many rows? "
ncols:   .asciiz  "How many columns? "

          
## writeBox -- writes rows of the box
##
## Input:
## $a1 -- number of rows
## Registers:
## $t0 - loop counter

         .text
         .globl writeBox

writeBox:
         # prolog
         sub     $sp,$sp,4        # push the return address
         sw      $ra,($sp)        # push $ra
         sub     $sp,$sp,4
         sw      $t0,($sp)        # push $t0
         
         # body
         li      $t0,0            # initialize $t0
loop:            
         bge      $t0,$a1,endloop # for ( i=1; i <= $a1 ;i++ )
         jal     writeRow         # current max in $v0
         la      $a0,nl
         li      $v0,4            # service 4
         syscall                  # newline
         addu    $t0,$t0,1        # incrament counter
         b       loop
endloop:
         # epilog                 
         lw      $t0,($sp)        # pop $t0 
         add     $sp,$sp,4                                    
         lw      $ra,($sp)        # pop return address
         add     $sp,$sp,4
         jr      $ra              # return to caller

         .data
nl:      .asciiz  "\n"

## writeRow -- writes a row of stars
##
## Input:
## $a1 -- number of rows
## Registers:
## $t0 - loop counter

         .text
         .globl writeRow

writeRow:  
         # prolog
         sub     $sp,$sp,4
         sw      $t0,($sp)        # push $t0

         # body
         li      $t0,0            # initialize $t0
         li      $v0,4            # Service 4
         la      $a0,star         # Address for star symbol
for:            
         bge     $t0,$a2,endfor   # for ( i=1; i <= $t0 ;i++ )
         syscall
         addu    $t0,$t0,1        # incrament counter
         b       for
endfor:
         # epilog
         lw      $t0,($sp)        # pop $t0 
         add     $sp,$sp,4 
         jr     $ra               # return to caller
         .data
star:    .asciiz  "*"
